package com.amin.concurrent;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class TestReadWriteLockDemo1 {
	public static String name = "李雷";
	public static String gender = "男";
	
	public static void main(String[] args) {
		
		ReadWriteLock lock = new ReentrantReadWriteLock();
		new Thread(new SynRun5(lock)).start();
		new Thread(new SynRun6(lock)).start();
	}
	
}

class SynRun5 implements Runnable{
	private ReadWriteLock lock;
	
	public SynRun5(ReadWriteLock lock) {
		super();
		this.lock = lock;
	}
	public void run() {
		while(true){
			lock.writeLock().lock();;
			if("李雷".equals(TestReadWriteLockDemo1.name)){
				TestReadWriteLockDemo1.name = "韩梅梅";
				TestReadWriteLockDemo1.gender = "女";
			}else{
				TestReadWriteLockDemo1.name = "李雷";
				TestReadWriteLockDemo1.gender = "男";
			}
			lock.writeLock().unlock();
		}
	}
}
class SynRun6 implements Runnable{
	private ReadWriteLock lock;
	
	public SynRun6(ReadWriteLock lock) {
		super();
		this.lock = lock;
	}

	public void run() {
		while(true){
			lock.readLock().lock();//开始锁定
			System.out.println(TestReadWriteLockDemo1.name+","+TestReadWriteLockDemo1.gender);
			lock.readLock().unlock();//释放锁
		}
		
	}
}